import type { IUserInfo } from './types/types';
import { STORAGE_USER_INFO } from './../static/js/constants/storage';
import { STORE_USER } from '@/static/js/constants/store';
import { useStorage } from '@/static/js/hooks/useStorage';
import { defineStore } from 'pinia';

export const useUserInfoStore = defineStore(STORE_USER, () => {
  const { localData: userInfo, removeStorage } =
    useStorage<IUserInfo>(STORAGE_USER_INFO);

  function setUserInfo(user: IUserInfo) {
    userInfo.value = user;
  }

  // 用户是否登录
  function isLogin() {
    let token = userInfo.value?.token;
    return token && token != 'null' && token != 'undefined';
  }

  function clearUserInfo() {
    removeStorage();
  }

  return { userInfo, setUserInfo, isLogin, clearUserInfo };
});
